//
//  SettingPwdViewController.m
//  parent
//
//  Created by 闫珍 on 15/8/5.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "SettingPwdViewController.h"
#import "AFAppClient.h"
#import "Tool.h"
#import "Config.h"
#import "MyloginViewController.h"

@interface SettingPwdViewController ()
@property (weak, nonatomic) IBOutlet UITextField *pwdTxt;
@property (weak, nonatomic) IBOutlet UITextField *nickname;

@end

@implementation SettingPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"注册";
    [self CustomBackButton];
}

- (IBAction)submit:(id)sender {
    NSString *pwdStr = _pwdTxt.text;
    NSString *nickStr = _nickname.text;
    if([pwdStr isEqualToString:@""]){
        [self showHudComplete:@"请输入密码" success:NO];
        return;
    }
    if([nickStr isEqualToString:@""]){
        [self showHudComplete:@"请输入昵称" success:NO];
        return;
    }
    if([pwdStr length]<6){
        [self showHudComplete:@"请保证密码6位数以上" success:NO];
        return;
    }
    
    NSDictionary *param = @{@"phone_num":_phone,@"code":_code,@"password":pwdStr,@"sex":@"1",@"device_token":@"0",@"nickname":nickStr};
    [self showHud:@"正在发送" isDim:YES];
    [[AFAppClient sharedManager]getWithURL:[NSString stringWithFormat:@"%@%@",SERVER_URL_NEW,kPhoneRegister] withParameters:param success:^(NSDictionary *responseObject) {
        NSUInteger code =[[responseObject objectForKey:@"code"]integerValue];
        if(code>0){
            [self hideHud2];
            NSUInteger user_id = [Tool defaultIntFromDic:responseObject withKey:@"result"];
            [[Config Instance] saveUID:user_id];
            [self loginWithUsername:[NSString stringWithFormat:@"%d",user_id] password:@"zhy611"];
            [Config Instance].isLogin = YES;
            [[Config Instance] saveCookie:YES];
            [self.navigationController popToRootViewControllerAnimated:YES];
            
            [Tool reSetInfo];
            
            [[[MyloginViewController shareController] navigationController]dismissViewControllerAnimated:YES completion:^{
                
            }];
            
        }else{
            NSString *result = [responseObject objectForKey:@"result"];
            [self showHudComplete:result success:NO];
        }
    } failure:^(NSError *error) {
        [self showHudComplete:kNoNetwork success:NO];
    }];
}
//点击登陆后的操作
- (void)loginWithUsername:(NSString *)username password:(NSString *)password
{
    //[self showHudInView:self.view hint:NSLocalizedString(@"login.ongoing", @"Is Login...")];
    //异步登陆账号
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:username
                                                        password:password
                                                      completion:
     ^(NSDictionary *loginInfo, EMError *error) {
         [self hideHud2];
         if (loginInfo && !error) {
             //设置是否自动登录
             [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
             
             // 旧数据转换 (如果您的sdk是由2.1.2版本升级过来的，需要家这句话)
             [[EaseMob sharedInstance].chatManager importDataToNewDatabase];
             //获取数据库中数据
             [[EaseMob sharedInstance].chatManager loadDataFromDatabase];
             
             //获取群组列表
             [[EaseMob sharedInstance].chatManager asyncFetchMyGroupsList];
             
             //发送自动登陆状态通知
             [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@YES];
             //保存最近一次登录用户名
         }
         else
         {
             switch (error.errorCode)
             {
                 case EMErrorNotFound:
                     TTAlertNoTitle(error.description);
                     break;
                 case EMErrorNetworkNotConnected:
                     TTAlertNoTitle(NSLocalizedString(@"error.connectNetworkFail", @"No network connection!"));
                     break;
                 case EMErrorServerNotReachable:
                     TTAlertNoTitle(NSLocalizedString(@"error.connectServerFail", @"Connect to the server failed!"));
                     break;
                 case EMErrorServerAuthenticationFailure:
                     TTAlertNoTitle(error.description);
                     break;
                 case EMErrorServerTimeout:
                     TTAlertNoTitle(NSLocalizedString(@"error.connectServerTimeout", @"Connect to the server timed out!"));
                     break;
                 default:
                     TTAlertNoTitle(NSLocalizedString(@"login.fail", @"Login failure"));
                     break;
             }
         }
     } onQueue:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
